package models

import (
	"github.com/astaxie/beego/orm"
	"strconv"
)

type AiTask struct {
	Id                 int64
	Tk_name            string
	Tk_level           int
	Tk_start_time      string
	Tk_end_time        string
	Tk_type            int
	Tk_state           int
	Tk_param1          int
	Tk_owner           int
	Tk_desc            string
	Tk_out_number      string
	Tk_tpl_id          int64
	Tk_max_call_time   int
	Tk_gateway_group   int64
	Tk_num_group       int64
	Tk_work_start_time string
	Tk_work_end_time   string
	Serverip           string
	Tk_call_interval   int
}

type AiSeatTask struct {
	Id               int64
	Tk_name          string
	Tk_start_time    string
	Tk_end_time      string
	Tk_type          int
	Tk_state         int
	Tk_owner         int
	Tk_gateway_group int64
	Tk_num_group     int64
}

func (a *AiTask) TableName() string {
	return TableName("task")
}

func (a *AiSeatTask) TableName() string {
	return TableName("seat_task")
}

func AiTaskAdd(a *AiTask) (int64, error) {
	return orm.NewOrm().Insert(a)
}

func AiSeatTaskAdd(a *AiSeatTask) (int64, error) {
	return orm.NewOrm().Insert(a)
}

func AiTaskGetByName(taskName string,owner int) (*AiTask, error) {
	a := new(AiTask)
	err := orm.NewOrm().QueryTable(TableName("task")).Filter("tk_name", taskName).Filter("tk_owner",owner).One(a)
	if err != nil {
		return nil, err
	}
	return a, nil
}
func AiTaskGetByName1(taskName string) (*AiSeatTask, error) {
	a := new(AiSeatTask)
	err := orm.NewOrm().QueryTable(TableName("seat_task")).Filter("tk_name", taskName).One(a)
	if err != nil {
		return nil, err
	}
	return a, nil
}

func AiTaskGetList(page, pageSize int, filters ...interface{}) ([]*AiTask, int64) {
	offset := (page - 1) * pageSize
	list := make([]*AiTask, 0)
	query := orm.NewOrm().QueryTable(TableName("task"))
	if len(filters) > 0 {
		l := len(filters)
		for k := 0; k < l; k += 2 {
			query = query.Filter(filters[k].(string), filters[k+1])
		}
	}
	total, _ := query.Count()
	query.OrderBy("-id").Limit(pageSize, offset).All(&list)

	return list, total
}

//座席
func AiSeatTaskGetList(page, pageSize int, filters ...interface{}) ([]*AiSeatTask, int64) {
	offset := (page - 1) * pageSize
	list := make([]*AiSeatTask, 0)
	query := orm.NewOrm().QueryTable(TableName("seat_task"))
	if len(filters) > 0 {
		l := len(filters)
		for k := 0; k < l; k += 2 {
			query = query.Filter(filters[k].(string), filters[k+1])
		}
	}
	total, _ := query.Count()
	query.OrderBy("-id").Limit(pageSize, offset).All(&list)

	return list, total
}

func TaskIdName() ([]*AiTask, error) {
	list := make([]*AiTask, 0)
	query := orm.NewOrm().QueryTable(TableName("task"))
	_, err := query.OrderBy("-id").All(&list)

	if err != nil {
		return nil, err
	}
	return list, nil
}
func AiTaskGetById(id int) (*AiTask, error) {
	r := new(AiTask)
	err := orm.NewOrm().QueryTable(TableName("task")).Filter("id", id).One(r)
	if err != nil {
		return nil, err
	}
	return r, nil
}
func AiSeatTaskGetById(id int) (*AiSeatTask, error) {
	r := new(AiSeatTask)
	err := orm.NewOrm().QueryTable(TableName("seat_task")).Filter("id", id).One(r)
	if err != nil {
		return nil, err
	}
	return r, nil
}

//根据id修改seattask
func ASeatTaskGetById(id int) (*AiSeatTask, error) {
	r := new(AiSeatTask)
	err := orm.NewOrm().QueryTable(TableName("seat_task")).Filter("id", id).One(r)
	if err != nil {
		return nil, err
	}
	return r, nil
}
func AiTaskStop(id int64, i int) error {
	sqlStr := "UPDATE ai_task SET tk_state=" + strconv.Itoa(i) + " WHERE id=" + strconv.FormatInt(id, 10)
	_, err := orm.NewOrm().Raw(sqlStr).Exec()
	if err != nil {
		ErrorLog(err)
		return err
	}

	return nil
}

func (a *AiTask) Update(fields ...string) error {
	if _, err := orm.NewOrm().Update(a, fields...); err != nil {
		return err
	}
	return nil
}

func (a *AiSeatTask) Update(fields ...string) error {
	if _, err := orm.NewOrm().Update(a, fields...); err != nil {
		return err
	}
	return nil
}

func (a *AiTask) Delete() error {
	if _, err := orm.NewOrm().Delete(a); err != nil {
		return err
	}
	return nil
}
func (a *AiSeatTask) Delete() error {
	if _, err := orm.NewOrm().Delete(a); err != nil {
		return err
	}
	return nil
}

//获取任务数量
func GetTaskCount(userId int) (int, error) {
	var authStr = " "
	if userId != 1 {
		authStr = " and tk_owner=" + strconv.Itoa(userId)
	}
	sqlStr := "select count(1) " +
		"from  ai_task " +
		"where TO_CHAR(tk_start_time,'yyyy-MM-dd') > TO_CHAR((current_timestamp - interval '1 day'),'yyyy-MM-dd') " + authStr + ";"

	var taskCount int
	err := orm.NewOrm().Raw(sqlStr).QueryRow(&taskCount)
	if err != nil {
		ErrorLog(err)
		return 0, err
	}

	return taskCount, nil
}
